package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.SoundService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserLikeManager {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Reference
    private SoundService soundService;

    @Reference
    private UserLikeService userLikeService;

    @Reference
    private FriendService friendService;

    @Reference
    private UserInfoService userInfoService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;


    //卡片推荐--喜欢
    public void saveUserLike(Long likeUserId) {

        Long userId = UserHolder.get().getId();

        //在user-like表保存喜欢信息
        userLikeService.save(userId, likeUserId);

        //删除这条语音
        String objectId = stringRedisTemplate.opsForValue().get("sound_objectId");
        soundService.deleteSound(likeUserId,objectId);
        stringRedisTemplate.delete("sound_objectId");

        //查询两人是否互相喜欢
        boolean isMutualLike = userLikeService.isMutualLike(userId, likeUserId);

        //如果互相喜欢
        if (isMutualLike) {
            //在friend表中添加两人信息
            friendService.save(userId,likeUserId);
            //在环信中添加两人好友关系
            huanXinTemplate.addContacts(userId,likeUserId);
        }

    }


    //卡片推荐--不喜欢
    public void deleteUserLike(Long likeUserId) {

        Long userId = UserHolder.get().getId();

        //删除我--喜欢--他
        userLikeService.delete(userId, likeUserId);

        //删除两人好友关系
        friendService.delete(userId, likeUserId);

        //删除两人环信中的好友关系
        huanXinTemplate.deleteContacts(userId, likeUserId);

    }


    //喜欢统计
    public UserLikeCountVo findUserCount() {

        Long userId = UserHolder.get().getId();

        //获取三个喜欢总数
        return userLikeService.findUserCount(userId);

    }


    //喜欢详情
    public PageBeanVo findUserList(Integer type, Integer pageNum, Integer pageSize) {

        Long userId = UserHolder.get().getId();

        //查询
        PageBeanVo pageBeanVo = userLikeService.findUserList(userId, type, pageNum, pageSize);

        //从pageBeanVo中获取items
        List<UserLikeVo> userLikeVoList = (List<UserLikeVo>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(userLikeVoList)) {
            for (UserLikeVo userLikeVo : userLikeVoList) {
                //根据userLikeVo获取用户id
                UserInfo userInfo = userInfoService.findById(userLikeVo.getId());

                //复制用户信息
                BeanUtil.copyProperties(userInfo,userLikeVo);
            }
        }

        //封装
        pageBeanVo.setItems(userLikeVoList);

        return pageBeanVo;
    }
}
